<script setup>
import {reactive, ref} from "vue";
import {User, Lock} from "@element-plus/icons-vue";
import { useRouter } from 'vue-router'
import {register} from '@/api/common/user.js'
import { showMessage } from "@/composables/util.js";
const router = useRouter()

// 定义响应式的表单对象
const form = reactive({
  username: '',
  password: '',
  confirmPass: '',
  role: ''
})

// 表单引用
const formRef = ref(null)

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('确认密码不能为空'))
  } else if (value !== form.password) {
    callback(new Error("两次密码输入不一致"))
  } else {
    callback()
  }
}

// 表单验证规则
const rules = {
  username: [
    {
      required: true,
      message: '用户名不能为空',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur',
    }
  ],
  confirmPass: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePass
    }
  ],
  role: [
    {
      required: true,
      message: '必须选择一个角色',
      trigger: 'change',
    }
  ]
}


const onSubmit = () => {
  console.log('注册')
  formRef.value.validate((valid) => {
    if (!valid) {
      console.log('表单验证不通过')
      return false
    }

    // 向后端调用注册接口
    register(form.username, form.password, form.role).then((res) => {
      // 判断注册是否成功
      if (res.data.success === true) {
        // 提示注册成功
        showMessage('注册成功')
        // 跳转登录页面
        router.push('/login')

      } else {
        let message = res.data.message
        console.log(message)
        // 提示消息
        showMessage(message, 'error')
      }
    })
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="illustration">
        <img
            src="../../assets/register.png"
            alt="注册插画"
            class="floating-image"
        >
        <div class="decorative-circle"></div>
      </div>

      <div class="form-wrapper">
        <el-form
            ref="formRef"
            :rules="rules"
            :model="form"
            class="animated-form"
        >
          <h1 class="login-title">
            <span class="welcome-text">欢迎注册</span>
            <div class="animated-dot">~</div>
          </h1>

          <!-- 用户名 -->
          <el-form-item prop="username">
            <div class="input-group">
              <el-input
                  v-model="form.username"
                  :prefix-icon="User"
                  size="large"
                  placeholder="请输入用户名"
              />
            </div>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <div class="input-group">
              <el-input
                  v-model="form.password"
                  :prefix-icon="Lock"
                  size="large"
                  show-password
                  placeholder="请输入密码"
              />
            </div>
          </el-form-item>

          <!-- 确认密码 -->
          <el-form-item prop="confirmPass">
            <div class="input-group">
              <el-input
                  v-model="form.confirmPass"
                  :prefix-icon="Lock"
                  size="large"
                  show-password
                  placeholder="请确认密码"
              />
            </div>
          </el-form-item>

          <!-- 身份 -->
          <el-form-item prop="role">
            <el-radio-group v-model="form.role" class="original-role-group">
              <el-radio value="BLOGGER" size="large">博主</el-radio>
              <el-radio value="USER" size="large">用户</el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- 注册按钮 -->
          <el-form-item>
            <el-button
                type="primary"
                class="login-btn"
                @click="onSubmit"
            >
              立即注册
              <span class="arrow">→</span>
            </el-button>
          </el-form-item>

          <div class="register-link">
            已有账号?
            <span @click="router.push('/login')">立即登录</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(
      145deg,
      rgba(3, 136, 117, 0.51) -20%,
      rgba(13, 107, 74, 0.55) 40%,
      rgba(255,255,255,0.8) 120%
  );
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 300vw;
    height: 300vw;
    background: radial-gradient(circle, rgba(64,158,255,0.05) 0%, transparent 60%);
    animation: gradient-rotate 20s linear infinite;
  }
}

.login-box {
  display: flex;
  width: 60%;
  max-width: 1000px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(64,158,255,0.15);
  overflow: hidden;
  transform: translateY(0);
  animation: box-float 4s ease-in-out infinite;
}

.illustration {
  flex: 1;
  position: relative;
  overflow: hidden;

  .floating-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease-out;
  }

  .decorative-circle {
    position: absolute;
    width: 200px;
    height: 200px;
    background: rgba(64,158,255,0.1);
    border-radius: 50%;
    filter: blur(60px);
    bottom: -50px;
    right: -50px;
  }
}

.form-wrapper {
  flex: 1;
  padding: 40px;
  position: relative;
}

.original-role-group {
  width: 70%;
  display: flex;
  justify-content: space-between;
  gap: 15px;

  :deep(.el-radio) {
    margin: 0;
    height: 40px;
    line-height: 40px;

    .el-radio__label {
      font-size: 18px;
      padding-left: 8px;
      transition: color 0.3s;
    }

    .el-radio__inner {
      width: 18px;
      height: 18px;
      border-width: 1px;

      &::after {
        width: 8px;
        height: 8px;
      }
    }

    &.is-checked {
      .el-radio__inner {
        background-color: #038875;
        border-color: #038875;
      }
      .el-radio__label {
        color: #038875;
        font-weight: 500;
      }
    }
  }
}

.login-title {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  color: #038875;
  .welcome-text {
    display: inline-block;
    font-size: 24px;
    font-weight: 600;
    color: #038875;
    transform: translateY(0);
    animation: text-float 3s ease-in-out infinite;
  }

  .animated-dot {
    display: inline-block;
    animation: dot-bounce 1.2s infinite;
  }
}

.input-group {
  position: relative;
  margin-bottom: 24px;

  :deep(.el-input__wrapper) {
    background: #f8fbff;
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 0 0 2px rgba(64,158,255,0.3);
    }

    &.is-focus {
      box-shadow: 0 0 0 2px rgba(64,158,255,0.5);
    }
  }

}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s ease !important;
  background-color: #038875 !important;
  border-color: #038875 !important;
  .arrow {
    opacity: 0;
    transform: translateX(-10px);
    transition: all 0.3s ease;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(64,158,255,0.3);
    background-color: #02bea2 !important;
    border-color: #02bea2 !important;
    .arrow {
      opacity: 1;
      transform: translateX(0);
    }
  }
}

/* 统一输入元素宽度 */
.input-group,
.login-btn {
  width: 100%;
}

.register-link {
  text-align: center;
  color: #606266;
  margin-top: 24px;

  span {
    color: #038875;
    cursor: pointer;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      bottom: -2px;
      left: 0;
      width: 0;
      height: 1px;
      background: #038875;
      transition: width 0.3s ease;
    }

    &:hover::after {
      width: 100%;
    }
  }
}

:deep(.el-form-item) {
  .el-form-item__error {
    top: -18px;
    &::before {
      content: "⚠ ";
    }
  }

}

/* 动画定义 */
@keyframes box-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

@keyframes gradient-rotate {
  from { transform: rotate(0); }
  to { transform: rotate(360deg); }
}

@keyframes text-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

@keyframes dot-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}
</style>